<template>
    <div>
        <a-table :dataSource="leaveBillList"
                 :columns="columns"
                 :pagination="pagination"
                 @change="handlerChange"
                 style="margin-top: 10px"
                 bordered>
            <template #user="{text,record}">
                {{beLongUser(text)}}
            </template>
            <template #state="{text,record}">
                <a-tag v-if="text == 0" color="processing">
                    <template #icon>
                        <sync-outlined :spin="true"/>
                    </template>
                    审批中
                </a-tag>
                <a-tag v-if="text == 1" color="success">
                    <template #icon>
                        <check-circle-outlined/>
                    </template>
                    审批通过
                </a-tag>
                <a-tag v-if="text == 2" color="error">
                    <template #icon>
                        <close-circle-outlined/>
                    </template>
                    申请失败
                </a-tag>
            </template>
            <template v-slot:action="{text,record}">
                <a-space>
                    <a-popconfirm
                            title="删除数据无法恢复，请确认是否删除？"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button v-if="record.state != 0" type="danger">删除</a-button>
                    </a-popconfirm>
                    <a-button type="primary"
                              v-if="record.state != 2"
                              @click="checkAuditTrail(record)">
                        查看审核记录
                    </a-button>
                </a-space>
            </template>
        </a-table>
        <a-drawer
                title="请假申请单"
                :width="1000"
                :visible="leaveVisible"
                :body-style="{ paddingBottom: '40px' }"
                :footer-style="{ textAlign: 'right' }"
                @close="onClose"
        >
            <a-form :model="drawerData.leaveBill" layout="vertical">
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item label="标题" name="title">
                            <a-input v-model:value="drawerData.leaveBill.title" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="请假天数" name="day">
                            <a-input v-model:value="drawerData.leaveBill.day" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="请假原因" name="remark">
                            <a-textarea v-model:value="drawerData.leaveBill.remark"
                                        :disabled="true" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
            <a-table :dataSource="drawerData.comments"
                     :columns="commentColumns"
                     :pagination="false"
                     bordered>
                <template #user="{text,record}">
                    {{beLongUser(text)}}
                </template>
                <template #time="{text,record}">
                    {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
                </template>
            </a-table>
        </a-drawer>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message} from 'ant-design-vue'
    import {SessionStorage} from '../../../public/session-storage'
    import {CheckCircleOutlined, CloseCircleOutlined, SyncOutlined} from "@ant-design/icons-vue";

    export default {
        name: "sales-leaveBillAll",
        components: {
            CloseCircleOutlined,
            SyncOutlined,
            CheckCircleOutlined
        },
        setup() {
            const columns = [
                {
                    title: 'ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '标题',
                    dataIndex: 'title',
                    key: 'title',
                    align: 'center'
                },
                {
                    title: '请假人',
                    dataIndex: 'userId',
                    key: 'userId',
                    slots: {customRender: 'user'},
                    align: 'center'
                },
                {
                    title: '天数',
                    dataIndex: 'day',
                    key: 'day',
                    align: 'center'
                },
                {
                    title: '请假原因',
                    dataIndex: 'remark',
                    key: 'remark',
                    align: 'center',
                    width: '30%'
                },
                {
                    title: '状态',
                    dataIndex: 'state',
                    key: 'state',
                    slots: {customRender: 'state'},
                    align: 'center'
                },
                {
                    title: '当前进度',
                    dataIndex: 'progress',
                    key: 'progress',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center',
                    width: '20%'
                }
            ]
            const commentColumns = [
                {
                    title: '时间',
                    dataIndex: 'time',
                    key: 'time',
                    slots: {customRender: 'time'}
                },
                {
                    title: '批注人',
                    dataIndex: 'userId',
                    key: 'userId',
                    slots: {customRender: 'user'}
                },
                {
                    title: '批注信息',
                    dataIndex: 'fullMessage',
                    key: 'fullMessage',
                },
            ]

            const loginUser = ref({})
            const leaveBill = ref({})
            const leaveBillList = ref()
            const userList = ref()
            const pagination = ref({
                current: 1,
                pageSize: 6,
            })
            const leaveVisible = ref(false)
            const drawerData = ref({})

            const checkAuditTrail = (record) => {

                if (record.state == 1) {
                    axios.get('task/overAuditTrail/leave/' + record.id).then(rs => {
                        drawerData.value = rs.data.data
                        leaveVisible.value = true
                    })
                } else if (record.state == 0) {
                    axios.get('task/currentAuditTrail/leave/' + record.id).then(rs => {
                        drawerData.value = rs.data.data
                        leaveVisible.value = true
                    })
                }
            }

            const onClose = () => {
                leaveVisible.value = false
            }

            const load = () => {
                axios.get('/leaveBill/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    leaveBillList.value = rs.data.data.list
                    leaveBillList.value.forEach((val)=>{
                        if (val.state == 0) {
                            axios.get('task/getCurrentProgress/leave/' + val.id).then(res => {
                                val.progress = res.data.data
                            })
                        }else {
                            val.progress = '该申请已结束'
                        }
                    })
                    pagination.value.total = rs.data.data.total
                })
            }

            const handlerChange = (pageInfo) => {
                pagination.value = pageInfo
                load()
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/leaveBill/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const beLongUser = (userId) => {
                let rs = ''
                userList.value.forEach(val => {
                    if (val.id == userId) {
                        rs = val.username
                        return rs
                    }
                })
                return rs == '' ? userId : rs
            }

            onMounted(() => {
                loginUser.value = SessionStorage.get('loginUser')
                axios.get('user/queryAll').then(rs => {
                    userList.value = rs.data.data
                })
                load()
            })
            return {
                columns,
                pagination,
                handlerChange,
                load,
                okDelete,
                leaveBill,
                leaveBillList,
                beLongUser,
                commentColumns,
                checkAuditTrail,
                leaveVisible,
                drawerData,
                onClose,
            }
        }
    }
</script>

<style scoped>

</style>